package com.inossem.warehousestorage_inossem.activity.in_storage.chuangyou;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.InStorageResultActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.extra.InStorageExtra;
import com.inossem.warehousestorage_inossem.adapter.inStorage.chuangyou.CYProductionOverViewAdapter;
import com.inossem.warehousestorage_inossem.bean.inStorage.chuangyou.CYInStorageProductionOverviewBean;
import com.inossem.warehousestorage_inossem.bean.inStorage.chuangyou.CYInStorageProductionSubmitRequestBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.inStorage.chuangyou.CYInStorageProductionConfirmRequest;
import com.inossem.warehousestorage_inossem.request.inStorage.chuangyou.CYInStorageProductionOverviewRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.CYInStorageUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;

import java.util.List;

/**
 * TODO
 *
 * @author 王斯宇
 * @time on 2018/4/3 15:47
 * @email siyu.wang@inossem.com
 */
public class CYInStorageProductionOverViewActivity extends BaseActivity {

    private TextView title , nextStep;

    private PullToRefreshLayout refresh;

    private PullableListView materielList;

    private CYProductionOverViewAdapter adapter;

    private List<CYInStorageProductionOverviewBean.OverViewBean.ItemListBean> allData;

    private String stockInputId , status;

    private String mCondition;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        super.onCreateImpl(savedInstanceState);
        setContentView(R.layout.cy_activity_in_storage_shopping_list);
        SPUtils.removeInStorageProductionOverViewList(CYInStorageProductionOverViewActivity.this);
        initView(CYInStorageProductionOverViewActivity.this);
        initData(CYInStorageProductionOverViewActivity.this);

    }

    @Override
    protected void onResumeImpl() {
        super.onResumeImpl();
        mCondition = "";
        getData(CYInStorageProductionOverViewActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        nextStep = (TextView) activity.findViewById(R.id.nextStep);
        materielList = (PullableListView) activity.findViewById(R.id.materielList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.materielListRefresh);
    }
    private void initData(Activity activity) {
        title.setText("生产入库概览");
        ActivityUtils.initBackAndFunction(activity, getResources().getString(R.string.activity_receive_result_back), new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                DialogUtils.getConfirmDialog(CYInStorageProductionOverViewActivity.this, getResources().getString(R.string.activity_in_storage_receive_overview_clear_prompt), new DialogUtils.ConfirmDialogListener() {
                    @Override
                    public void onConfirm(Dialog dialog) {
                        finish();
                    }
                }).show();

            }
        });
        getExtra(activity);
        if (status.equals(InStorageExtra.STATUS_PRUDUCTION_COMPLETE)) {
            nextStep.setText("提交");
        }else if(status.equals(InStorageExtra.STATUS_NO_INSTORAGE)){
            nextStep.setText("过账");
        }
        nextStep.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (CYInStorageUtils.allItemIsDone(activity)) {
                    productionCompleteConfirm(activity , status);
                }
            }
        });
        materielList.setPullUpFlag(false);
        materielList.setPullDownFlag(false);
        getData(activity);
//        setmScanListener((String scanResult, int length) -> {
//            mCondition = QRCodeUtils.DecryptQRCode(scanResult);
//        }, QRCodeConstant.TYPE_MATERIEL_CODE);
    }
    private void getExtra(Activity activity) {
        stockInputId = activity.getIntent().getStringExtra(InStorageExtra.STOCK_INPUT_ID);
        status = activity.getIntent().getStringExtra(InStorageExtra.STATUS);
    }
    private void addInStorageShoppingItemListData (Context context, List<CYInStorageProductionOverviewBean.OverViewBean.ItemListBean> list , CYInStorageProductionOverviewBean.OverViewBean bean , ListView listView, boolean flag) {
        if (flag) {
            adapter = new CYProductionOverViewAdapter(context , mCondition  , list , bean , new InStorageShoppingOverviewListener() {
                @Override
                public void onNoDoubleClickListener(String batch) {
                   mCondition = batch;
                   getData(context);
                }

                @Override
                public void onViewClickListener(String matId) {
                    Intent intent = new Intent();
                    if (status.equals(InStorageExtra.STATUS_PRUDUCTION_COMPLETE)) {
                        intent.setClass(context , CYInStorageProductionCompleteMaterielDetailActivity.class);
                        intent.putExtra(InStorageExtra.MAT_ID , matId);
                    }else if(status.equals(InStorageExtra.STATUS_NO_INSTORAGE)){
                        intent.setClass(context , CYInStorageNoInStorageMaterielDetailActivity.class);
                        intent.putExtra(InStorageExtra.MAT_ID , matId);
                    }
                    startActivity(intent);
                }
            });
            listView.setAdapter(adapter);
        }
    }
    private void getData(Context context) {
        String allocatingInStorageInformation = SPUtils.getInStorageProductionOverViewlList(context);
        if (TextUtils.isEmpty(allocatingInStorageInformation)) {
            CYInStorageProductionOverviewRequest.request(context, stockInputId, new GsonBeanListener<CYInStorageProductionOverviewBean>() {
                @Override
                public void onSuccess(CYInStorageProductionOverviewBean bean) {
                    Gson gson = new Gson();
                    String json = gson.toJson(bean, CYInStorageProductionOverviewBean.class);
                    SPUtils.saveInStorageProductionOverViewlList(context, json);
                    CYInStorageProductionOverviewBean.OverViewBean data = bean.getBody();
                    allData = bean.getBody().getItem_list();
                    addInStorageShoppingItemListData(context,  ActivityUtils.getShowData(allData, 0), data,materielList, true);
                }
            });
        } else {
            Gson gson = new Gson();
            CYInStorageProductionOverviewBean bean = gson.fromJson(allocatingInStorageInformation, CYInStorageProductionOverviewBean.class);
            if (TextUtils.isEmpty(mCondition)) {
                allData = bean.getBody().getItem_list();
            } else {
                allData = CYInStorageUtils.getProductionInStorageList(context, mCondition);
            }
            addInStorageShoppingItemListData(context, ActivityUtils.getShowData(allData, 0),bean.getBody(),  materielList, true);
        }
    }

    private void productionCompleteConfirm(Context context , String status) {
        CYInStorageProductionSubmitRequestBean params = CYInStorageUtils.getProductionInStorageSubmitBean(context);
//        Gson gson = new Gson();
//        LogUtils.i(" params ==== " + gson.toJson(params));
        CYInStorageProductionConfirmRequest.request(context,status, params,new GsonBeanListener<CYInStorageShoppingConfirmBean>() {
            @Override
            public void onSuccess(CYInStorageShoppingConfirmBean bean) {
//                if (SUCCESS.equals(bean.getBody().getCode())) {
                SPUtils.removeInStorageProductionOverViewList(CYInStorageProductionOverViewActivity.this);
                Intent intent = new Intent(context, InStorageResultActivity.class);
                intent.putExtra(InStorageExtra.MESSAGE, bean.getHead().getMsg());
                intent.putExtra(InStorageExtra.CONFIRM_MESSAGE, context.getResources().getString(R.string.activity_in_storage_receive_confirm_success));
                startActivity(intent);
            }

            @Override
            public void onFailing(CYInStorageShoppingConfirmBean bean) {
                if (!TextUtils.isEmpty(bean.getHead().getMsg())) {
                    Utils.middleToast(context, bean.getHead().getMsg());
                }
            }
        });
    }

    public interface InStorageShoppingOverviewListener {
        void onNoDoubleClickListener(String batch);
        void onViewClickListener (String matId);
    }
    @Override
    public void onBackPressed() {
        DialogUtils.getConfirmDialog(CYInStorageProductionOverViewActivity.this, getResources().getString(R.string.activity_in_storage_receive_overview_clear_prompt), new DialogUtils.ConfirmDialogListener() {
            @Override
            public void onConfirm(Dialog dialog) {
                CYInStorageProductionOverViewActivity.super.onBackPressed();
            }
        }).show();
    }

}
